<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="到货单号" prop="daohuono">-->
      <!--        <el-input v-model="queryParams.daohuono" placeholder="请输入到货单号"-->
      <!--                  clearable @keyup.enter.native="handleQuery"/>-->
      <!--      </el-form-item>-->
      <el-form-item label="到货单号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入到货单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="到货日期">
        <el-date-picker v-model="dateRange" style="display: flex;" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="采购人" prop="lvBuycreateUsername">
        <el-input v-model="queryParams.lvBuycreateUsername" placeholder="请输入采购人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="制单人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入制单人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="质检人" prop="zhijianName">
        <el-input v-model="queryParams.zhijianName" placeholder="请输入质检人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货人" prop="shourenName">
        <el-input v-model="queryParams.shourenName" placeholder="请输入收货人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="goodModel">
        <el-input v-model="queryParams.goodModel" placeholder="请输产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column sortable label="到货单号" align="center" prop="lvDaohuo.nos" show-overflow-tooltip />
      <el-table-column sortable label="到货日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <!--<span>{{ parseTime(scope.row.lvDaohuo.arrivalDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="到货类型" align="center" prop="fahuoType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fahuo_type" :value="scope.row.fahuoType" />
        </template>
      </el-table-column>
      <el-table-column sortable label="采购人" align="center" prop="lvBuycreateUsername"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column sortable label="质检人" align="center" prop="zhijianName"/>
      <el-table-column sortable label="收货人" align="center" prop="shourenName"/>
      <el-table-column sortable label="供应商" align="center" prop="supplierName" show-overflow-tooltip />
      <el-table-column sortable label="产品编码" align="center" prop="goodsSalebuyall.goodsCode" />
      <el-table-column sortable label="产品名称" align="center" prop="goodsSalebuyall.goodsName" show-overflow-tooltip />
      <el-table-column sortable label="产品型号" align="center" prop="goodsSalebuyall.goodModel" show-overflow-tooltip />
      <el-table-column sortable label="到货数量" align="center" prop="daohuoCount" />
      <el-table-column sortable label="到货单价" align="center" prop="unitPrice" />
      <el-table-column sortable label="到货总价" align="center" prop="totalPrice" />
      <el-table-column sortable align="center" label="类型" prop="tuihuoType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fahuo_type" :value="scope.row.tuihuoType" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="入库状态" prop="storageState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.storage_state" :value="scope.row.storageState" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-document-checked" @click="inspect(scope.row)"-->
<!--            v-hasPermi="['develop:LvDaohuoDetail:edit']">质检单-->
<!--          </el-button>-->
<!--          <el-button size="mini" type="text" icon="el-icon-document" @click="handleList(scope.row)"-->
<!--            v-hasPermi="['develop:LvDaohuoDetail:edit']">检验批次-->
<!--          </el-button>-->


          <el-button size="mini" type="text" icon="el-icon-set-up" v-if="scope.row.goods.type ==='1'"
                     @click="qualityMaterialTesting(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">材料质检
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-set-up" v-if="scope.row.goods.type === '0'"
                     @click="qualityTesting(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">产品质检
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <zhijiandan-form ref="zhijiandanForm" @ok="getList"></zhijiandan-form>
    <ProductInspectionBatchFormInfo @ok="getList" ref="productInspectionBatch" />

    <factory-test-quality ref="qualityTesting" @ok="getList"> </factory-test-quality>
    <factory-test-quality-all ref="qualityTestingAll" @ok="getList"> </factory-test-quality-all>
  </div>
</template>

<script>
  import {
    deleteAction,
    getAction
  } from '@/api/manage'
  import {
    list
  } from "@/api/develop/lvDaohuoDetail";
  import ZhijiandanForm from "@/views/develop/lvBuy/module/ZhijiandanForm";
  import ProductInspectionBatchFormInfo from '@/views/develop/goodsSale/module/ProductInspectionBatchFormInfo';
  import FactoryTestQualityAll from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityAll.vue";
  import FactoryTestQuality from "@/views/develop/factory/factoryTestQuality/FactoryTestQuality.vue";

  export default {
    name: "YirukuList",
    dicts: ['inspect_state', 'storage_state', 'fahuo_type'],
    components: {
      FactoryTestQuality, FactoryTestQualityAll,
      ZhijiandanForm,
      ProductInspectionBatchFormInfo
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 采购单表格数据
        dataList: [],
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          params: {},
          nos: null,
          arrivalDate: null,
          supplierName: null,
          inspectState: "1",
          storageState: "1",
          goodsCode: null,
          goodsName: null,
          goodModel: null,
          lvBuycreateUsername: null,
          createBy: null,
          zhijianName: null,
          shourenName: null,
          orderByColumn: "createTime",
          isAsc: "desc"
        },
        url: {
          list: '/lvDaohuoDetail/lvDaohuoDetail/list',
          ruku: '/lvDaohuoDetail/lvDaohuoDetail/ruku/',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    created() {
      this.getList();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight)
    },
    methods: {
      /** 查询采购单列表 */
      getList() {
        this.loading = true;
        list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
       /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      inspect(row) {
        this.$refs.zhijiandanForm.inspectShow(row);
      },
      /** 检验批次 */
      qualityMaterialTesting(row){
        this.$refs.qualityTestingAll.showByDaohuoDetail(row);
      },

      qualityTesting(row){
        this.$refs.qualityTesting.showByDaohuoDetail(row);
      },
      /** 入库按钮操作 */
      handleRuku(row) {
        const ids = row.id || this.ids;
        getAction(this.url.ruku + ids, {}).then(response => {
          this.getList();
          this.$modal.msgSuccess("入库成功！");
        }).catch(() => {});
      },
      handlerukuAll() {
        const ids = this.ids;
        getAction(this.url.ruku + ids, {}).then(response => {
          this.getList();
          this.$modal.msgSuccess("入库成功！");
        }).catch(() => {});
      },
    }
  };
</script>
